<template>
  <div class="room-list-container">
    <el-drawer v-model="drawerVisible" direction="rtl" size="30%">
      <Screening @filter="handleFilter" />
    </el-drawer>
    <el-dialog 
      v-model="reservationDialogVisible" 
      width="65%"
      :fullscreen="false"
      class="reservation-dialog"
    >
      <Reservation 
        v-if="reservationDialogVisible"
        :room-id="currentRoom?.id"
        :room-name="currentRoom?.name"
        @close="reservationDialogVisible = false"
      />
    </el-dialog>
    <div class="header">
      <div class="header-content">
        <div class="title-section">
          <h2>会议室列表</h2>
          <div class="room-count">
            <el-tag type="info" effect="plain">{{ filteredRooms.length }} 个会议室</el-tag>
          </div>
        </div>
        <el-button type="primary" size="large" class="filter-button" @click="drawerVisible = true">
          <el-icon class="button-icon"><Filter /></el-icon>
          筛选
        </el-button>
      </div>
    </div>
    <div class="card-container">
      <el-card v-for="room in filteredRooms" :key="room.id" class="room-card">
        <div class="card-header">
          <h3>{{ room.name }}</h3>
        </div>
        <div class="card-content">
          <div class="info-item">
            <el-icon><House /></el-icon>
            <span>{{ room.type === 'classroom' ? '教室型' : room.type === 'round_table' ? '圆桌型' : room.type }}</span>
          </div>
          <div class="info-item">
            <el-icon><User /></el-icon>
            <span>容量: {{ room.capacity }}人</span>
          </div>
          <div class="info-item">
            <el-icon><ScaleToOriginal /></el-icon>
            <span>面积: {{ room.area }}㎡</span>
          </div>
          <div class="info-item">
            <el-icon><Money /></el-icon>
            <span>价格: ¥{{ room.hourlyRate }}/小时</span>
          </div>
          <div class="equipment-section">
            <h4>设备配置</h4>
            <div class="equipment-tags">
              <el-tag v-for="(item, index) in room.equipments" :key="index" class="equipment-tag">
                <el-icon class="equipment-icon">
                  <component :is="getEquipmentIcon(item)" />
                </el-icon>
                {{ getEquipmentName(item) }}
              </el-tag>
            </div>
          </div>
        </div>
        <div class="card-footer">
          <el-button 
            type="primary" 
            class="reserve-button"
            @click="showReservationDialog(room)"
          >
            <el-icon><Calendar /></el-icon>
            预约会议室
          </el-button>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import Screening from './Screening.vue'
import Reservation from './Reservation.vue'
import { useConferenceStore } from '../stores/ConferenceStore'
import { 
  Filter, 
  House, 
  User, 
  ScaleToOriginal, 
  Money, 
  Calendar,
  VideoCamera,
  Microphone,
  Connection,
  Edit
} from '@element-plus/icons-vue'

const conferenceStore = useConferenceStore()

const drawerVisible = ref(false)
const reservationDialogVisible = ref(false)
const currentRoom = ref(null)
const rooms = ref([])
const filterConditions = ref({})

const getEquipmentIcon = (equipment) => {
  const icons = {
    projector: VideoCamera,
    audio: Microphone,
    network: Connection,
    whiteboard: Edit
  }
  return icons[equipment] || House
}

const getEquipmentName = (equipment) => {
  const names = {
    projector: '投影仪',
    audio: '音响系统',
    network: '网络设备',
    whiteboard: '白板'
  }
  return names[equipment] || equipment
}

const fetchRooms = async () => {
  // 输入筛选条件，获取会议室列表
  rooms.value = await conferenceStore.getConferenceList(filterConditions.value)
}

const filteredRooms = computed(() => {
  // 这里实现前端筛选逻辑作为后备方案
  return rooms.value
})

const handleFilter = (conditions) => {
  filterConditions.value = conditions
  fetchRooms()
  drawerVisible.value = false
}

const showReservationDialog = (room) => {
  currentRoom.value = room
  reservationDialogVisible.value = true
}

onMounted( async () => {
  await fetchRooms()
})
</script>

<style scoped>
.room-list-container {
  padding: 0;
  margin: 0;
  width: 100%;
  height: 100%;
  background-color: #f5f7fa;
  position: relative;
}

.header {
  position: fixed;
  top: 0;
  left: 240px; /* 为侧栏留出空间 */
  right: 0;
  z-index: 1000;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.header-content {
  max-width: 1600px;
  margin: 0 auto;
  padding: 16px 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.title-section {
  display: flex;
  align-items: center;
  gap: 16px;
}

.header h2 {
  margin: 0;
  font-size: 24px;
  color: #1a237e;
  font-weight: 600;
  background: linear-gradient(90deg, #1a237e 0%, #3949ab 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.room-count {
  display: flex;
  align-items: center;
}

.filter-button {
  display: flex;
  align-items: center;
  gap: 8px;
  background: linear-gradient(90deg, #3949ab 0%, #5c6bc0 100%);
  border: none;
  padding: 12px 24px;
  font-size: 14px;
  font-weight: 500;
  border-radius: 8px;
  transition: all 0.3s ease;
  box-shadow: 0 4px 12px rgba(57, 73, 171, 0.2);
}

.filter-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(57, 73, 171, 0.3);
}

.button-icon {
  font-size: 16px;
}

.card-container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 24px;
  padding: 24px;
  margin-top: 80px;
  max-width: 1600px;
  margin-left: 12px;
  margin-right: auto;
}

.room-card {
  background: white;
  border-radius: 16px;
  transition: all 0.3s ease;
  border: none;
  overflow: hidden;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  position: relative;
  display: flex;
  flex-direction: column;
}

.room-card:hover {
  transform: translateY(-8px);
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.12);
}

.room-card.is-available {
  border: 2px solid transparent;
  background: linear-gradient(white, white) padding-box,
              linear-gradient(90deg, #3949ab 0%, #5c6bc0 100%) border-box;
}

.card-header {
  padding: 20px;
  background: linear-gradient(90deg, #3949ab 0%, #5c6bc0 100%);
  color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-header h3 {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
}

.status-tag {
  padding: 4px 12px;
  border-radius: 12px;
  font-weight: 500;
}

.card-content {
  padding: 24px;
  flex: 1;
}

.info-item {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
  color: #546e7a;
  font-size: 14px;
}

.info-item .el-icon {
  color: #3949ab;
  font-size: 18px;
}

.equipment-section {
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid rgba(0, 0, 0, 0.05);
}

.equipment-section h4 {
  margin: 0 0 12px 0;
  color: #1a237e;
  font-size: 16px;
  font-weight: 600;
}

.equipment-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.equipment-tag {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 12px;
  border-radius: 8px;
  background-color: #e8eaf6;
  color: #3949ab;
  border: none;
  transition: all 0.3s ease;
}

.equipment-tag:hover {
  background-color: #c5cae9;
  transform: translateY(-2px);
}

.equipment-icon {
  font-size: 14px;
}

.card-footer {
  padding: 20px 24px;
  text-align: center;
  border-top: 1px solid rgba(0, 0, 0, 0.05);
  background: transparent;
}

.reserve-button {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 24px;
  font-size: 15px;
  font-weight: 500;
  border-radius: 8px;
  background: linear-gradient(90deg, #3949ab 0%, #5c6bc0 100%);
  border: none;
  color: white;
  transition: all 0.3s ease;
  box-shadow: 0 4px 12px rgba(57, 73, 171, 0.2);
}

.reserve-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(57, 73, 171, 0.3);
  background: linear-gradient(90deg, #1a237e 0%, #3949ab 100%);
}

.reserve-button .el-icon {
  font-size: 18px;
}

@media (max-width: 1200px) {
  .card-container {
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  }
}

@media (max-width: 768px) {
  .header {
    left: 0; /* 移动端下不需要为侧栏留空间 */
  }

  .header-content {
    flex-direction: column;
    gap: 16px;
    padding: 12px 16px;
  }

  .title-section {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }

  .card-container {
    grid-template-columns: 1fr;
    padding: 16px;
    margin-top: 120px;
  }

  .filter-button {
    width: 100%;
    justify-content: center;
  }
}

.reservation-dialog :deep(.el-dialog) {
  max-width: 1000px;
  margin: 0 auto;
}

.reservation-dialog :deep(.el-dialog__body) {
  padding: 0;
}

@media (max-width: 768px) {
  .reservation-dialog :deep(.el-dialog) {
    width: 90% !important;
    margin: 0 auto;
  }
}
</style>